package com.bbs2.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.bbs2.entity.ConcernPost;
import com.bbs2.service.ConcernPostService;
import com.bbs2.util.SysConst;

public class ConcernPostAction implements SessionAware,ServletRequestAware,ServletResponseAware {
	private Map<String, Object> session;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private String postId;
	private String userId;
	
	public String execute() throws IOException{
		PrintWriter out = response.getWriter();
		ConcernPostService cps = new ConcernPostService();
		if(postId != null && userId != null){
			boolean isNum1 = Pattern.matches("\\d+", userId);
			boolean isNum2 = Pattern.matches("\\d+", postId);
			if(isNum1 && isNum2){
				boolean isValid = cps.checkConcernPost(userId, postId);
				if(isValid){
					ConcernPost cp = new ConcernPost();
					cp.setFlag(SysConst.ENABLE);
					cp.setUserId(Integer.parseInt(userId));
					cp.setPostId(Integer.parseInt(postId));
					int rows = cps.save(cp);
					if(rows>0){
						JSONObject json = JSONObject.fromObject(cp);
						out.print(json.toString());
						return "";
					}else{
						return "error";
					}
				}else{
					out.print("isExist");
					return "";
				}
			}else{
				return "error";
			}
		}else{
			return "error";
		}
	}
	
	public String getPostId() {
		return postId;
	}

	public void setPostId(String postId) {
		this.postId = postId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}
}
